package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.cache.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.domain.*;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.repository.StrategyRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
//@Transactional mongodb的事务必须是复制集的方式
public  class StrategyServiceImpl implements IStrategyService{
    @Autowired
    private StrategyRepository repository;

    @Autowired
    private MongoTemplate template;

    @Autowired
    private IStrategyCatalogService strategyCatalogService;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Override
    public void save(Strategy strategy) {
        strategy.setId(null);//把id设置为null
        repository.save(strategy);//存入strategy对象

    }

    @Override
    public void update(Strategy strategy) {
        repository.save(strategy);//编辑和保存是一样的,更新之后则要保存strategy对象

    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);

    }

    @Override
    public Strategy get(String id) {
        //orEls,查询id,存在就返回,不承诺在就返回null
        return repository.findById(id).orElse(null);

    }

    @Override
    public List<Strategy> list() {
        //查询所有
        return repository.findAll();
    }

    @Override
    public Page<Strategy> query(StrategyQuery qo) {

        //分页查询
        //查询对象
        Query query = new Query();
        if (StringUtils.hasLength(qo.getDestId())){
            query.addCriteria(Criteria.where("destId").is(qo.getDestId()));
        }

        if (StringUtils.hasLength(qo.getThemeId())){
            query.addCriteria(Criteria.where("themeId").is(qo.getThemeId()));
        }

        PageRequest pageable = PageRequest.of(qo.getCurrentPage() - 1,
                qo.getPageSize(), Sort.Direction.ASC, "_id");
        return DBHelper.query(template,Strategy.class,query,pageable);

    }

    @Override
    public void saveOrUpdate(Strategy strategy) {

        //目的地id
        StrategyCatalog catalog = strategyCatalogService.get(strategy.getCatalogId());
        strategy.setDestId(catalog.getDestId());
        //目的地name
        strategy.setDestName(catalog.getDestName());
        //主题名称
        StrategyTheme theme = strategyThemeService.get(strategy.getThemeId());
        strategy.setThemeName(theme.getName());
        //分类名称
        strategy.setCatalogName(catalog.getName());

        //是否是国外

        //中国>>广东>>广州
        List<Destination> toasts = destinationService.queryToasts(catalog.getDestId());
        if (toasts !=null && toasts.size() >0){
            Destination dest = toasts.get(0);
            if ("中国".equals(dest.getName())){
                //国内
                strategy.setIsabroad(Strategy.ABROAD_NO);
            }else{
                //国外
                strategy.setIsabroad(Strategy.ABROAD_YES);
            }
        }

        if (StringUtils.hasLength(strategy.getId())){
            //1.查询
            Strategy temp = this.get(strategy.getId());
            //2.替换
            strategy.setCreateTime(temp.getCreateTime());
            strategy.setViewnum(temp.getViewnum());
            strategy.setCreateTime(temp.getCreateTime());
            strategy.setReplynum(temp.getReplynum());
            strategy.setThumbsupnum(temp.getThumbsupnum());
            strategy.setFavornum(temp.getFavornum());
            strategy.setSharenum(temp.getSharenum());
            //update//特殊案例,页面更新的数据,刚好跟集合中字段个数一致
            //所以这种等价于全量的更新
            //3.更新
            this.update(strategy);
        }else {
            //save
            this.save(strategy);
        }

    }

    @Override
    public void changeState(String id, int state) {
        //部分进行更新
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        Update update = new Update();
        update.set("state",state); //state=1 出现问题
        template.updateMulti(query,update,Strategy.class);
    }

    @Override
    public List<Strategy> queryCatalogId(String id) {
        return repository.findByCatalogId(id);
    }

    @Override
    public List<Strategy> queryViewnumTop3(String destId) {
        //1.目的地id必须指定
        //2.点击量前三(倒叙)
        //3.截取前面三个
        Pageable pageable = PageRequest.of(0,3,
                Sort.Direction.DESC,"viewnum");

      List<Strategy> list = repository.findByDestId(destId,pageable);
        return list;
    }

    @Override
    public void updateStrategyVo(StrategyStatisVO vo) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(vo.getStrategyId()));


        Update update = new Update();
        update.set("viewnum",vo.getViewnum());
        update.set("replynum",vo.getReplynum());
        update.set("favornum",vo.getFavornum());
        update.set("sharenum",vo.getSharenum());
        update.set("thumbsupnum",vo.getThumbsupnum());
        template.updateMulti(query,update,Strategy.class);

    }


}
